extends ../main

block append body
    div.form-element
        div.col-lg-12(style="margin-top:40px")
            div.panel.form-element-padding.panel-info
                div.panel-heading
                    h3#header.panel-title 菜单添加
                div.panel-body
                    div.col-lg-12(style="padding-top:20px;")
                        form.form-horizontal
                            div.form-group
                                label.col-sm-1.control-label.text-center(for="menuName") 菜单名称
                                div.col-sm-3
                                    input#menuName.form-control(type="text",placeholder="菜单名称",v-model="menuName")
                                    input#menuId(type="hidden")
                                label.col-sm-1.control-label.text-center(for="menuKey") 菜单键值
                                div.col-sm-3
                                    input#menuKey.form-control(type="text",placeholder="菜单键值",v-model="menuKey")
                                label.col-sm-1.control-label.text-center(for="url") 菜单URL
                                div.col-sm-3
                                    input#url.form-control(type="text",placeholder="菜单URL",v-model="url")
                            div.form-group
                                label.col-sm-1.control-label.text-center(for="method") 菜单方法
                                div.col-sm-3
                                    input#method.form-control(type="text",placeholder="菜单方法",v-model="method")
                                label.col-sm-1.control-label.text-center(for="parentId") 上级菜单
                                div.col-sm-3
                                    select#parentId.form-control(v-model="parentId")
                                label.col-sm-1.control-label.text-center(for="sort") 排序
                                div.col-sm-3
                                    input#sort.form-control(type="text",placeholder="排序",v-model="sort")

                            div.form-group
                                label.col-sm-1.control-label.text-center 是否登陆
                                div.col-sm-3
                                    label 是
                                        input(type="radio",v-model="isLogin",value="1")
                                    label 否
                                        input(type="radio",v-model="isLogin",value="0")
                                label.col-sm-1.control-label.text-center 是否显示
                                div.col-sm-3
                                    label 是
                                        input(type="radio",v-model="isDisplay",value="1")
                                    label 否
                                        input(type="radio",v-model="isDisplay",value="0")
                    div.form-group.col-sm-offset-4
                        div.col-sm-1
                            input.btn.btn-info(type="button",value="返回",onclick="location.href='/manager/main/menuInfo'")
                        div.col-sm-1
                            input#submit.btn.btn-primary(type="button",value="提交更改",@click="submitData")

block append footer
    script(type="text/javascript").
        $(function() {

            var menu = '#{menu}';



            if (menu == '' || menu == undefined) {
                menu = '';
            } else {
                menu = JSON.parse(HtmlUtil.htmlDecodeByRegExp(menu));
            }

            console.log(menu);

            var app = new Vue({
                el: "#content",
                data: {
                    id:"",
                    menuName:"",
                    url:"",
                    method:"",
                    parentId:"",
                    parOptions:[{id:0,menuName:""}],
                    menuKey:"",
                    isDisplay:0,
                    sort:0,
                    isLogin:1
                },
                methods: {
                    init: function () {

                        zxlib.fpost("/manager/main/getParent", null, function (data) {
                            console.log(data)
                            app.parOptions = data.data;
                            $("#parentId").select2({
                                placeholder: "请选择",
                                allowClear: true,
                                data:$.map(app.parOptions,function (obj) {
                                    obj.id = obj.id || obj.id;
                                    obj.text = obj.text || obj.menuName; // replace name with the property used for the text
                                    return obj;
                                })
                            });

                            if (menu != '' && menu != undefined) {
                                app.id = menu.id;
                                app.menuName = menu.menuName;
                                app.url = menu.url;
                                app.method = menu.method;
                                //$("#parentId").val(5).trigger("change");
                                $("#parentId").val(menu.parentId).select2();
                                app.parentId = menu.parentId;
                                app.menuKey = menu.menuKey;
                                app.isDisplay = menu.isDisplay;
                                app.sort = menu.sort;
                                app.isLogin = menu.isLogin;
                            } else {
                                $("#parentId").val(null).select2();
                            }
                        }, function (data) {
                            zxlib.showAlert("error", data);
                        });

                    },
                    submitData:function () {
                        var p = {
                            menuId:app.id,
                            menuName:app.menuName,
                            url:app.url,
                            method:app.method,
                            parentId:$("#parentId").val() == ''?app.parentId : $("#parentId").val(),
                            menuKey:app.menuKey,
                            isDisplay:app.isDisplay,
                            sort:app.sort,
                            isLogin:app.isLogin
                        }
                        zxlib.fpost("/manager/main/addMenus",p,function (data) {
                            zxlib.showAlert("success",data.msg,1000,function () {
                                app.id = "";
                                app.menuName = "";
                                app.url = "";
                                app.method = "";
                                $("#parentId").val(null).select2();
                                app.menuKey = "";
                                app.isDisplay = 0;
                                app.sort = 0;
                                app.isLogin = 1;
                            });

                        },function (data) {
                            zxlib.showAlert("error",data);
                        });
                    }
                }
            });
            app.init();
        });